{
 "cells": [
  {
   "cell_type": "code",
   "execution_count": 45,
   "id": "381a1589-5d68-4179-8b87-c00f3a3a4093",
   "metadata": {},
   "outputs": [],
   "source": [
    "import requests\n",
    "import pandas as pd"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 133,
   "id": "a3558083-3da7-49f1-8802-15f147576003",
   "metadata": {},
   "outputs": [],
   "source": [
    "def get_fund_div_data(symbol):\n",
    "    \n",
    "    url = 'https://stock.finance.sina.com.cn/fundInfo/api/openapi.php/FundPageInfoService.tabfh'\n",
    "\n",
    "    data_input = {\n",
    "        'symbol' : symbol, \n",
    "        'format' : 'json',\n",
    "    }\n",
    "\n",
    "    resp = requests.get(url, params=data_input)\n",
    "    data = resp.json()\n",
    "    div = pd.DataFrame(data['result']['data']['fhdata']).astype({'mffh':float})\n",
    "    \n",
    "    div = div[div['mffh'] > 0.0]\n",
    "    \n",
    "    div = div.drop('fhr', axis=1)\n",
    "\n",
    "    RENAME = {\n",
    "        'djr' : 'date',\n",
    "        'mffh' : 'amount',\n",
    "    }\n",
    "\n",
    "    div = div.rename(columns = RENAME)\n",
    "\n",
    "    div['date'] = pd.to_datetime(div['date'])\n",
    "   \n",
    "    # sort div data frame from old to new\n",
    "    div = div.set_index('date').sort_index()\n",
    "    \n",
    "    return div"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 134,
   "id": "82b60fbc-c778-4232-ab72-ec49e604c85a",
   "metadata": {},
   "outputs": [],
   "source": [
    "div = get_fund_div_data('020005')"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 135,
   "id": "1e2027ea-3424-454e-872a-f08fee4d88d1",
   "metadata": {},
   "outputs": [
    {
     "data": {
      "text/html": [
       "<div>\n",
       "<style scoped>\n",
       "    .dataframe tbody tr th:only-of-type {\n",
       "        vertical-align: middle;\n",
       "    }\n",
       "\n",
       "    .dataframe tbody tr th {\n",
       "        vertical-align: top;\n",
       "    }\n",
       "\n",
       "    .dataframe thead th {\n",
       "        text-align: right;\n",
       "    }\n",
       "</style>\n",
       "<table border=\"1\" class=\"dataframe\">\n",
       "  <thead>\n",
       "    <tr style=\"text-align: right;\">\n",
       "      <th></th>\n",
       "      <th>amount</th>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>date</th>\n",
       "      <th></th>\n",
       "    </tr>\n",
       "  </thead>\n",
       "  <tbody>\n",
       "    <tr>\n",
       "      <th>2006-03-10</th>\n",
       "      <td>0.0300</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>2006-09-01</th>\n",
       "      <td>0.1000</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>2018-01-16</th>\n",
       "      <td>0.1146</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>2018-06-20</th>\n",
       "      <td>0.0550</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>2020-01-13</th>\n",
       "      <td>0.1310</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>2021-01-18</th>\n",
       "      <td>0.2680</td>\n",
       "    </tr>\n",
       "  </tbody>\n",
       "</table>\n",
       "</div>"
      ],
      "text/plain": [
       "            amount\n",
       "date              \n",
       "2006-03-10  0.0300\n",
       "2006-09-01  0.1000\n",
       "2018-01-16  0.1146\n",
       "2018-06-20  0.0550\n",
       "2020-01-13  0.1310\n",
       "2021-01-18  0.2680"
      ]
     },
     "execution_count": 135,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "div"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "id": "f76dba7c-1f99-4539-908a-1d4d65f5d562",
   "metadata": {},
   "outputs": [],
   "source": []
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "id": "00067554-0953-4ebe-a72a-8f1ae25f87fd",
   "metadata": {},
   "outputs": [],
   "source": []
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "id": "a4ac8ad5-721e-4a57-bd75-9f6ba0a87b9c",
   "metadata": {},
   "outputs": [],
   "source": []
  },
  {
   "cell_type": "code",
   "execution_count": 11,
   "id": "016716d4-ae35-44f1-818b-38e06f02f3ab",
   "metadata": {},
   "outputs": [
    {
     "name": "stderr",
     "output_type": "stream",
     "text": [
      "/home/azureuser/miniconda3/envs/fundopt/lib/python3.9/site-packages/arctic-1.80.0-py3.9.egg/arctic/store/_pandas_ndarray_store.py:8: FutureWarning: The Panel class is removed from pandas. Accessing it from the top-level namespace will also be removed in the next version\n"
     ]
    }
   ],
   "source": [
    "from arctic import Arctic, CHUNK_STORE\n",
    "lib = Arctic('localhost')\n",
    "fund = lib.get_library('fund')"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 83,
   "id": "79284cc5-6ee5-466d-914c-16b2e429cf73",
   "metadata": {},
   "outputs": [],
   "source": [
    "df = fund.read('020005')"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 13,
   "id": "d7849db9-e258-4f67-befe-cb3f86952a4c",
   "metadata": {},
   "outputs": [],
   "source": [
    "import datetime"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 69,
   "id": "2b1eee78-db3f-414a-b373-a7344094c8f4",
   "metadata": {},
   "outputs": [],
   "source": [
    "div_date = datetime.date(2021, 1, 18)"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 70,
   "id": "078caf04-3d5e-47df-84cb-15b4df304579",
   "metadata": {},
   "outputs": [],
   "source": [
    "df.loc[div_date:, 'NAV'] += 0.268"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 73,
   "id": "1f630ec5-0740-4973-8240-d43f28a991b9",
   "metadata": {},
   "outputs": [],
   "source": [
    "diff = df.diff()"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 74,
   "id": "1aed2383-6403-42cd-9a65-6e2f7d13d945",
   "metadata": {},
   "outputs": [
    {
     "data": {
      "text/plain": [
       "<AxesSubplot:xlabel='NAV', ylabel='ACC_NAV'>"
      ]
     },
     "execution_count": 74,
     "metadata": {},
     "output_type": "execute_result"
    },
    {
     "data": {
      "image/png": "iVBORw0KGgoAAAANSUhEUgA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ys94e36/gUeDMoPlqDPGah0jeULIQyY5bgAuB7gnHzgUOMbP3iK/nsw/xtaHg301RZwKL3L0hZ5GKpEHJQiQL3P0j4AHiCQMziwFnAaPcfai7DyW+01lTU9QzxFec/S5qgpI8pGQhkj03Ak2joo4F1rn7uoTzzwMjg/0PGonv39AnOC6SV7TqrIiIhFLNQkREQilZiIhIKCULEREJpWQhIiKhlCxERCSUkoWIiIRSshARkVD/H99mrCdOwmDUAAAAAElFTkSuQmCC\n",
      "text/plain": [
       "<Figure size 432x288 with 1 Axes>"
      ]
     },
     "metadata": {
      "needs_background": "light"
     },
     "output_type": "display_data"
    }
   ],
   "source": [
    "diff.plot.scatter('NAV', 'ACC_NAV')"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 78,
   "id": "9f513718-0e20-4d35-816e-a62ac2671e29",
   "metadata": {},
   "outputs": [
    {
     "data": {
      "text/plain": [
       "date\n",
       "2020-01-07         NaN\n",
       "2020-01-08         NaN\n",
       "2020-01-09         NaN\n",
       "2020-01-10         NaN\n",
       "2020-01-13         NaN\n",
       "                ...   \n",
       "2021-05-03   -0.016602\n",
       "2021-05-04   -0.006152\n",
       "2021-05-05    0.013691\n",
       "2021-05-06   -0.011885\n",
       "2021-05-07   -0.006344\n",
       "Name: NAV, Length: 349, dtype: float64"
      ]
     },
     "execution_count": 78,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "df.NAV.pct_change(freq='20B')"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 99,
   "id": "fe152117-3dde-45ea-8811-5569cd2de2a8",
   "metadata": {},
   "outputs": [],
   "source": [
    "def calc_return_ts(nav, div, start, end, offset):\n",
    "    div = div.loc[start:end]\n",
    "    nav = nav.loc[start:end]\n",
    "    for ex_date, row in div.itertuples(index=True):\n",
    "        nav.loc[ex_date:] += row.amount\n",
    "        \n",
    "    start_nav = nav.shift(offset)\n",
    "    end_nav = nav\n",
    "    \n",
    "    ret = (end_nav/start_nav) - 1\n",
    "    \n",
    "    return ret, nav"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 96,
   "id": "49435eda-1c41-4b0c-8080-5a0a603124d3",
   "metadata": {},
   "outputs": [],
   "source": [
    "start = datetime.date(2020, 1, 1)\n",
    "end = datetime.date(2021, 5, 7)"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 100,
   "id": "9b94bc14-0847-4e15-af08-0f81e74eeab3",
   "metadata": {},
   "outputs": [],
   "source": [
    "ret_df, nav = calc_return_ts(df['NAV'], div, start, end, 20)"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 98,
   "id": "603bc9ce-4ee5-45d7-b8fd-64622b783523",
   "metadata": {},
   "outputs": [
    {
     "data": {
      "text/plain": [
       "date\n",
       "2021-01-18   -0.048435\n",
       "2021-01-19   -0.069027\n",
       "2021-01-20   -0.071303\n",
       "2021-01-21   -0.039930\n",
       "2021-01-22   -0.042467\n",
       "                ...   \n",
       "2021-05-03   -0.019493\n",
       "2021-05-04   -0.007237\n",
       "2021-05-05    0.016162\n",
       "2021-05-06   -0.014009\n",
       "2021-05-07   -0.007503\n",
       "Name: NAV, Length: 80, dtype: float64"
      ]
     },
     "execution_count": 98,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "ret_df.loc[div_date:]"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 127,
   "id": "e8278e55-287f-4f1a-90a6-4622d719d261",
   "metadata": {},
   "outputs": [
    {
     "data": {
      "text/plain": [
       "date\n",
       "2021-01-18    2.010\n",
       "2021-01-19    1.977\n",
       "2021-01-20    1.988\n",
       "2021-01-21    2.034\n",
       "2021-01-22    2.045\n",
       "              ...  \n",
       "2021-05-03    1.908\n",
       "2021-05-04    1.908\n",
       "2021-05-05    1.908\n",
       "2021-05-06    1.877\n",
       "2021-05-07    1.854\n",
       "Name: NAV, Length: 80, dtype: float64"
      ]
     },
     "execution_count": 127,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "nav.loc[div_date:]"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 87,
   "id": "44c54f9d-4190-4f83-88b2-582535048d0d",
   "metadata": {},
   "outputs": [
    {
     "data": {
      "text/plain": [
       "date\n",
       "2021-05-03   -0.019493\n",
       "2021-05-04   -0.007237\n",
       "2021-05-05    0.016162\n",
       "2021-05-06   -0.014009\n",
       "2021-05-07   -0.007503\n",
       "Name: NAV, dtype: float64"
      ]
     },
     "execution_count": 87,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "ret_df.tail()"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 88,
   "id": "ae5845db-e395-4c7d-8cd8-9a70b2d1bcfd",
   "metadata": {},
   "outputs": [
    {
     "data": {
      "text/html": [
       "<div>\n",
       "<style scoped>\n",
       "    .dataframe tbody tr th:only-of-type {\n",
       "        vertical-align: middle;\n",
       "    }\n",
       "\n",
       "    .dataframe tbody tr th {\n",
       "        vertical-align: top;\n",
       "    }\n",
       "\n",
       "    .dataframe thead th {\n",
       "        text-align: right;\n",
       "    }\n",
       "</style>\n",
       "<table border=\"1\" class=\"dataframe\">\n",
       "  <thead>\n",
       "    <tr style=\"text-align: right;\">\n",
       "      <th></th>\n",
       "      <th>NAV</th>\n",
       "      <th>ACC_NAV</th>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>date</th>\n",
       "      <th></th>\n",
       "      <th></th>\n",
       "    </tr>\n",
       "  </thead>\n",
       "  <tbody>\n",
       "    <tr>\n",
       "      <th>2021-01-18</th>\n",
       "      <td>1.611</td>\n",
       "      <td>8.825</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>2021-01-19</th>\n",
       "      <td>1.578</td>\n",
       "      <td>8.693</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>2021-01-20</th>\n",
       "      <td>1.589</td>\n",
       "      <td>8.737</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>2021-01-21</th>\n",
       "      <td>1.635</td>\n",
       "      <td>8.921</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>2021-01-22</th>\n",
       "      <td>1.646</td>\n",
       "      <td>8.964</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>...</th>\n",
       "      <td>...</td>\n",
       "      <td>...</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>2021-05-03</th>\n",
       "      <td>1.509</td>\n",
       "      <td>8.418</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>2021-05-04</th>\n",
       "      <td>1.509</td>\n",
       "      <td>8.418</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>2021-05-05</th>\n",
       "      <td>1.509</td>\n",
       "      <td>8.418</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>2021-05-06</th>\n",
       "      <td>1.478</td>\n",
       "      <td>8.294</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>2021-05-07</th>\n",
       "      <td>1.455</td>\n",
       "      <td>8.203</td>\n",
       "    </tr>\n",
       "  </tbody>\n",
       "</table>\n",
       "<p>80 rows × 2 columns</p>\n",
       "</div>"
      ],
      "text/plain": [
       "              NAV  ACC_NAV\n",
       "date                      \n",
       "2021-01-18  1.611    8.825\n",
       "2021-01-19  1.578    8.693\n",
       "2021-01-20  1.589    8.737\n",
       "2021-01-21  1.635    8.921\n",
       "2021-01-22  1.646    8.964\n",
       "...           ...      ...\n",
       "2021-05-03  1.509    8.418\n",
       "2021-05-04  1.509    8.418\n",
       "2021-05-05  1.509    8.418\n",
       "2021-05-06  1.478    8.294\n",
       "2021-05-07  1.455    8.203\n",
       "\n",
       "[80 rows x 2 columns]"
      ]
     },
     "execution_count": 88,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "df.loc[div_date:]"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 121,
   "id": "bb98e3c5-42e9-405f-b86b-5e291eac1942",
   "metadata": {},
   "outputs": [],
   "source": [
    "div = div.loc[start:end]\n",
    "nav = nav.loc[start:end]\n",
    "for row in div.itertuples(index=True):\n",
    "    nav.loc[row.Index:] += row.amount"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 122,
   "id": "40c7cd4a-f951-4ac0-a75d-7dbb933a13e9",
   "metadata": {},
   "outputs": [
    {
     "data": {
      "text/html": [
       "<div>\n",
       "<style scoped>\n",
       "    .dataframe tbody tr th:only-of-type {\n",
       "        vertical-align: middle;\n",
       "    }\n",
       "\n",
       "    .dataframe tbody tr th {\n",
       "        vertical-align: top;\n",
       "    }\n",
       "\n",
       "    .dataframe thead th {\n",
       "        text-align: right;\n",
       "    }\n",
       "</style>\n",
       "<table border=\"1\" class=\"dataframe\">\n",
       "  <thead>\n",
       "    <tr style=\"text-align: right;\">\n",
       "      <th></th>\n",
       "      <th>payDate</th>\n",
       "      <th>amount</th>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>exDate</th>\n",
       "      <th></th>\n",
       "      <th></th>\n",
       "    </tr>\n",
       "  </thead>\n",
       "  <tbody>\n",
       "    <tr>\n",
       "      <th>2020-01-13</th>\n",
       "      <td>2020-01-14</td>\n",
       "      <td>0.131</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>2021-01-18</th>\n",
       "      <td>2021-01-19</td>\n",
       "      <td>0.268</td>\n",
       "    </tr>\n",
       "  </tbody>\n",
       "</table>\n",
       "</div>"
      ],
      "text/plain": [
       "              payDate  amount\n",
       "exDate                       \n",
       "2020-01-13 2020-01-14   0.131\n",
       "2021-01-18 2021-01-19   0.268"
      ]
     },
     "execution_count": 122,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "div"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 128,
   "id": "dc94f7a2-748e-4032-90c3-076122e67a0b",
   "metadata": {},
   "outputs": [],
   "source": [
    "start_nav = nav.shift(20)\n",
    "end_nav = nav\n",
    "\n",
    "ret = (end_nav/start_nav) - 1"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 129,
   "id": "7d9446c7-543b-4f03-8771-95eee6619f60",
   "metadata": {},
   "outputs": [
    {
     "data": {
      "text/plain": [
       "date\n",
       "2021-01-18    0.101974\n",
       "2021-01-19    0.082694\n",
       "2021-01-20    0.079262\n",
       "2021-01-21    0.109051\n",
       "2021-01-22    0.105405\n",
       "                ...   \n",
       "2021-05-03   -0.015480\n",
       "2021-05-04   -0.005732\n",
       "2021-05-05    0.012739\n",
       "2021-05-06   -0.011064\n",
       "2021-05-07   -0.005898\n",
       "Name: NAV, Length: 80, dtype: float64"
      ]
     },
     "execution_count": 129,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "ret.loc[div_date:]"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "id": "efa6d863-456a-49c8-8c7e-27991f009ee8",
   "metadata": {},
   "outputs": [],
   "source": []
  }
 ],
 "metadata": {
  "kernelspec": {
   "display_name": "Python 3",
   "language": "python",
   "name": "python3"
  },
  "language_info": {
   "codemirror_mode": {
    "name": "ipython",
    "version": 3
   },
   "file_extension": ".py",
   "mimetype": "text/x-python",
   "name": "python",
   "nbconvert_exporter": "python",
   "pygments_lexer": "ipython3",
   "version": "3.9.4"
  }
 },
 "nbformat": 4,
 "nbformat_minor": 5
}
